package com.sari.common.persistence;

import com.sari.common.utils.CacheUtils;

import java.util.*;

/**
 * 前端分页
 *
 */
public class ForeEndPage{

    public final static int pageSize = 5; // 页面大小，设置为“-1”表示不进行分页（分页无效）
    public static void initCachePage(String key,LinkedHashMap followsPatients){
        if(CacheUtils.get(key)==null){
            CacheUtils.put(key,followsPatients);
        }
    }

    public static void updateCachePage(String key, LinkedHashMap followsPatients){
        if(CacheUtils.get(key)!=null){
            CacheUtils.remove(key);
        }
        CacheUtils.put(key,followsPatients);
    }
    public static void clearCachePage(String key){
        CacheUtils.remove(key);
    }

    public static List getCachePageByNo(String key,int pageNo){
        List result = new LinkedList();
        Map pagesContent = (LinkedHashMap)CacheUtils.get(key);
        int first = pageNo*pageSize - pageSize;
        int end = first + pageSize;
        int i=0;
        int j = 0;
        Iterator<Map.Entry<String, String>> it = pagesContent.entrySet().iterator();
        while (it.hasNext()) {
            if(i>=first&&i<end){
                Map.Entry<String, String> entry = it.next();
                result.add(j++,entry.getValue());
            }
            i++;
        }
        return result;
    }
}
